#include "TimeTreeWidgetItem.h"

#include <QMouseEvent>
#include <QPainter>

TimeTreeWidgetHeader::TimeTreeWidgetHeader(QWidget *parent, Qt::WindowFlags f)
    : TimeTreeItemManager(parent, f)
    , _rectTextDelta(15, 10, -30, 0)
{
    QObject::connect(this, SIGNAL(signalSetDialXPos(int)), this, SLOT(slotSetDialXPos(int)));
    QObject::connect(this, SIGNAL(signalAddWheelValue(int)), this, SLOT(slotAddWheelValue(int)));
    this->setFixedHeight(_rectTextDelta.y() * 2 + s_leftRect.height());
}

TimeTreeWidgetHeader::~TimeTreeWidgetHeader()
{

}

void TimeTreeWidgetHeader::mouseReleaseEvent(QMouseEvent *event)
{
    TimeTreeItemManager::mouseReleaseEvent(event);
    if (event->button() == Qt::RightButton)
    {
        emit signalRightButtonRelease(event->globalPos());
    }
}

void TimeTreeWidgetHeader::wheelEvent(QWheelEvent *event)
{
    emit signalAddWheelValue(event->delta()/7);
}

void TimeTreeWidgetHeader::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);    // 反走样

    /* 绘制背景颜色 */
    painter.setPen(QPen(QColor("royalblue"), 3));    // 设置画笔颜色、宽度
    painter.setBrush(QColor("royalblue"));  // 设置画刷颜色 //darkcyan	#008B8B	0,139,139
    painter.drawRect(0, 0, this->width(), this->height());

    QRect rectText(_rectTextDelta.x() + s_leftRect.x(), _rectTextDelta.y() + s_leftRect.y(),
                    _rectTextDelta.width() + s_leftRect.width(), _rectTextDelta.height() + s_leftRect.height());
    /* 绘制矩形 */
    painter.setPen(QPen(QColor("white"), 3));    // 设置画笔颜色、宽度
    painter.setBrush(QColor("darkcyan"));  // 设置画刷颜色 //darkcyan	#008B8B	0,139,139
    painter.drawRect(rectText);

    /* 绘制文本 当前时间 */
    painter.setPen(QColor("white")); // 设置画笔颜色
    QFont font; // 设置字体：微软雅黑、点大小50、斜体
//    font.setFamily("Microsoft YaHei");
    font.setPointSize(13);
    painter.setFont(font);
    painter.drawText(QRect(rectText.x() + s_iTextSide, rectText.y(), rectText.width(), rectText.height()),
                     Qt::AlignLeft | Qt::AlignVCenter, QStringLiteral("当前时间"));
    int showTime = s_dCurDialValue * 10;
    painter.drawText(QRect(rectText.x() + s_iTextSide, rectText.y(), rectText.width() - s_iTextSide * 2, rectText.height()),
                     Qt::AlignRight | Qt::AlignVCenter,
                     QString::number(int(s_dCurDialValue)).append(".").append(QString::number(showTime % 10)).append(QStringLiteral("  秒")));

    /* 绘制刻度线 */
    painter.setPen(QPen(QColor("white"), 1.8));
    int xStartPos = rectText.width() + rectText.x() * 2; // 刻度线开始位置
    int yStartPos = rectText.y() + rectText.height();
    // 从开始位置开始，绘制5000，基础平面高度在40处，最短刻度为7，刻度间隔为5
    int dialHeight = rectText.height() / 4;
    int textRectSide = rectText.height(); // 字体大小
    int numScale = (this->width()- xStartPos) / s_iDialGap;
    for (int i = 0; i < numScale; ++i)
    {
        QString showNum;
        int xPos = xStartPos + i * s_iDialGap;
        int yHeight = dialHeight;
        int dialShow = i + s_iCurWheelValue;
        if (dialShow > s_iMaxDialValue)
        {
            break;
        }
        if (dialShow % 60 == 0)
        {
            yHeight = dialHeight * 3;
            if (dialShow / 60 > 0)
            {
                showNum = QString::number(dialShow/60).append(QStringLiteral("分"));
            }else {
                showNum = QString::number(dialShow);
            }
        }
        else if (dialShow % 5 == 0)
        {
            yHeight = dialHeight * 2;
            if (dialShow % 10 == 0)
            {
                showNum = QString::number(dialShow);
            }
        }
        if (!showNum.isEmpty())
        {
            painter.drawText(QRect(xPos-textRectSide, yStartPos - dialHeight * 3 - textRectSide * 2, textRectSide * 2, textRectSide * 2),
                                 Qt::AlignBottom | Qt::AlignHCenter, showNum);
        }
        painter.drawLine(QPoint(xPos, yStartPos), QPoint(xPos, yStartPos - yHeight));
    }

    /* 绘制滑块 */
    int curDialValue = s_dCurDialValue;
    if (curDialValue >= s_iCurWheelValue && curDialValue < s_iCurWheelValue + numScale)
    {
        painter.setPen(QPen(QColor("red"), 1.8));    // 设置画笔颜色、宽度
        painter.setBrush(QColor(255, 69, 0, 128));  // 设置画刷颜色 orangered	#FF4500	255,69,0
        int xAdd = (curDialValue - s_iCurWheelValue) * s_iDialGap;
        painter.drawRect(xStartPos + xAdd - s_iDialGap, yStartPos + 3, s_iDialGap * 2, -dialHeight * 3 - 3);
        painter.drawLine(QPoint(xStartPos + xAdd , yStartPos + 3), QPoint(xStartPos + xAdd, this->height()));
    }
}


TimeTreeWidgetItem::TimeTreeWidgetItem(QWidget *parent, Qt::WindowFlags f)
    : TimeTreeItemManager(parent, f)
    , _iLevelNum(1)
    , _bIsSelected(false)
    , _bIsExpend(true)
    , _bItemVisible(true)
{
    this->setFixedHeight(s_leftRect.height());
}

TimeTreeWidgetItem::~TimeTreeWidgetItem()
{

}

void TimeTreeWidgetItem::mouseReleaseEvent(QMouseEvent *event)
{
    TimeTreeItemManager::mouseReleaseEvent(event);
    if (event->button() == Qt::LeftButton)
    {
        emit signalClickedSelected(1);
    } else {
        emit signalClickedSelected(2);
    }
}

void TimeTreeWidgetItem::mouseDoubleClickEvent(QMouseEvent *event)
{
    TimeTreeItemManager::mouseDoubleClickEvent(event);
    if (event->pos().x() < s_leftRect.width())
    {
        _bIsExpend = !_bIsExpend;

        if (event->button() == Qt::LeftButton)
        {
            emit signalDoubleClickedSelected();
        }
    }
}

void TimeTreeWidgetItem::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);    // 反走样

    // 设置背景色
    painter.setPen(QPen(QColor("lightgray"), 1.8)); // lightgrey	#D3D3D3	211,211,211
    painter.setBrush(QColor("lightgray"));
    painter.drawRect(QRect(0, 0, this->width(), this->height()));

    drawLevelElement(&painter);

    QString strColor = "white";

    if (_bIsSelected)
    {
        /* 绘制圆点 */
        painter.setPen(QPen(QColor("white"), 3));    // 设置画笔颜色、宽度
        painter.setBrush(QColor("white"));  // 设置画刷颜色 //darkmagenta	#8B008B	139,0,139
        QRect rect(s_leftRect.x() + s_iTextSide * 3 + s_iTriangle ,  (s_leftRect.y() + s_leftRect.height()) / 2- s_iWhiteCircle / 2,
                   s_iWhiteCircle, s_iWhiteCircle);
        painter.drawEllipse(rect);
        strColor = "lightskyblue";
    }

    /* 绘制刻度底色为白色 */
    painter.setPen(QPen(QColor(strColor), 3));    // 设置画笔颜色、宽度
    painter.setBrush(QColor(strColor));  // 设置画刷颜色
    painter.drawRect(QRect(s_leftRect.x()+s_leftRect.width(), s_leftRect.y(), this->width(), s_leftRect.height()));

    /* 绘制文本 当前时间 */
    painter.setPen(QColor("white")); // 设置画笔颜色
    QFont font; // 设置字体：微软雅黑、点大小50、斜体
    font.setPointSize(13);
    painter.setFont(font);
    painter.drawText(QRect(s_leftRect.x() + s_iTextSide * 4 + s_iTriangle + s_iWhiteCircle, s_leftRect.y(), s_leftRect.width(), s_leftRect.height()),
                     Qt::AlignLeft | Qt::AlignVCenter, _strId);
    /* 绘制刻度线 */
    painter.setPen(QPen(QColor("lightgray"), 1.8)); // lightgrey	#D3D3D3	211,211,211
    int xStartPos = s_leftRect.width() + s_leftRect.x(); // 刻度线开始位置
    int numScale = (this->width()- xStartPos) / s_iDialGap;
    for (int i = 0; i < numScale; ++i)
    {
        int xPos = xStartPos + i * s_iDialGap;
        int dialShow = i + s_iCurWheelValue;
        if (dialShow > s_iMaxDialValue)
        {
            break;
        }
        painter.drawLine(QPoint(xPos, s_leftRect.y()), QPoint(xPos, s_leftRect.height()));
    }

    /* 绘制滑块 */
    int curDialValue = s_dCurDialValue;
    if (curDialValue >= s_iCurWheelValue && curDialValue < s_iCurWheelValue + numScale)
    {
        painter.setPen(QPen(QColor("red"), 1.8));    // 设置画笔颜色、宽度
        int xAdd = (curDialValue - s_iCurWheelValue) * s_iDialGap;
        painter.drawLine(QPoint(xStartPos + xAdd , s_leftRect.y()), QPoint(xStartPos + xAdd, this->height()));
    }
}

void TimeTreeWidgetItem::drawLevelElement(QPainter *painter)
{
    if (painter == Q_NULLPTR)
    {
        return;
    }

    if (_iLevelNum == 1)
    {
        /* 绘制矩形 */
        painter->setPen(QPen(QColor("darkmagenta"), 3));    // 设置画笔颜色、宽度
        painter->setBrush(QColor("darkmagenta"));  // 设置画刷颜色 //darkmagenta	#8B008B	139,0,139
        painter->drawRect(s_leftRect);

        /* 绘制三角形*/
        painter->setPen(QPen(QColor("white"), 3));    // 设置画笔颜色、宽度
        painter->setBrush(QColor("white"));  // 设置画刷颜色 //darkmagenta	#8B008B	139,0,139

        const QPointF points[6] = {
              QPointF(s_leftRect.x() + s_iTextSide, (s_leftRect.y() + s_leftRect.height()) / 2 - s_iTriangle / 2),
              QPointF(s_leftRect.x() + s_iTextSide, (s_leftRect.y() + s_leftRect.height()) / 2 + s_iTriangle / 2),
              QPointF(s_leftRect.x() + s_iTextSide + s_iTriangle * sin(3.14 / 3), (s_leftRect.y() + s_leftRect.height()) / 2),

              QPointF(s_leftRect.x() + s_iTextSide, (s_leftRect.y() + s_leftRect.height()) / 2 - s_iTriangle * sin(3.14 / 3) / 2),
              QPointF(s_leftRect.x() + s_iTextSide + s_iTriangle, (s_leftRect.y() + s_leftRect.height()) / 2 - s_iTriangle * sin(3.14 / 3) / 2),
              QPointF(s_leftRect.x() + s_iTextSide + s_iTriangle * sin(3.14 / 3) / 2, (s_leftRect.y() + s_leftRect.height()) / 2 + s_iTriangle * sin(3.14 / 3) /2 ),
          };


        if (!_bIsExpend)
        {
           painter->drawPolygon(points, 3);
        }else {
           painter->drawPolygon(points + 3, 3);
        }

    } else if ( _iLevelNum == 2)
    {
        /* 绘制矩形 */
        painter->setPen(QPen(QColor("darkorange"), 3));    // 设置画笔颜色、宽度
        painter->setBrush(QColor("darkorange"));  // 设置画刷颜色 //darkmagenta	#8B008B	139,0,139
        painter->drawRect(QRect(s_leftRect.x() + s_iTextSide + s_iTriangle, s_leftRect.y(), s_leftRect.width() - s_iTextSide - s_iTriangle, s_leftRect.height()));
    }
}
